package volkov.ja.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.Select;

public class MovieViewPage extends InternalPage {

	public MovieViewPage(PageManager pages) {
		super(pages);
	}

	@FindBy(tagName = "h2")
	private WebElement title;

	@FindBy(css = "div.notes']")
	private WebElement notes;

	@FindBy(css = "img[alt='Remove']")
	private WebElement removeButton;

	@FindBy(css = "img[alt='Edit']")
	private WebElement editButton;

	// private Select permissionDropdown() {
	// return new Select(driver.findElement(By.name("permission")));
	// }
	// public String getRole() {
	// return permissionDropdown().getFirstSelectedOption().getText();
	// }

	public String getTitle() {
		String compositeTitle = title.getText();
		return compositeTitle.substring(0, compositeTitle.length() - 7);
	}

	public String getYear() {
		String compositeTitle = title.getText();
		return compositeTitle.substring(compositeTitle.length() - 5,
				compositeTitle.length() - 1);
	}

	public String getNotes() {
		return title.getText();
	}

	@Override
	public String getPageRelativeUrl() {
		return "?go=movie";
	}

	public MovieListPage clickRemoveMovieLink() {
		removeButton.click();
		pages.getPageUtils().handleAlertIfPresent(true, "Couldn't confirm movie deletion.");
		return pages.movieListPage;
	}

	public MovieCreatePage clickEditMovieLink() {
		editButton.click();
		return pages.movieCreatePage;
	}

	public MovieViewPage ensurePageLoaded() {
		super.ensurePageLoaded();
		pages.getWait().until(presenceOfElementLocated(By.cssSelector("div#movie")));
		return this;
	}

}
